package template

import (
	"database/sql"
)

type UserDAO struct {
	tBaseDAO
	mItems []*UserInfo
}


func NewUserDAO() IUserDAO {
	it := &UserDAO{ *newBaseDAO(nil, nil), nil }
	it.fnBeforeQuery = it.BeforeQuery
	it.fnScanRow = it.ScanRow
	return it
}

func (me *UserDAO) BeforeQuery() error {
	me.mItems = make([]*UserInfo, 0)
	return nil
}

func (me *UserDAO) ScanRow(rows *sql.Rows) error {
	user := NewUserInfo()
	e := rows.Scan(&user.ID, &user.Name, &user.Pwd, &user.OrgID, &user.RoleID)
	if e != nil {
		return e
	}
	me.mItems = append(me.mItems, user)
	return nil
}


func (me *UserDAO) GetUserByID(db *sql.DB, id int) (error, *UserInfo) {
	e := me.QueryOne(db, "select id,name,pwd,org_id,role_id from user_info where id=?", id)
	if e != nil {
		return e, nil
	}
	return nil, me.mItems[0]
}

func (me *UserDAO) GetUsersByOrgID(db *sql.DB, orgID int) (error, []*UserInfo) {
	e := me.QueryMulti(db, "select id,name,pwd,org_id,role_id from user_info where org_id=?", orgID)
	if e != nil {
		return e, nil
	}
	return nil, me.mItems
}